//
//  BPBeforeLoginView.swift
//  BPWeiBo
//
//  Created by 扒皮大叔 on 16/4/7.
//  Copyright © 2016年 apple. All rights reserved.
//设置登陆之前的视图

//导入第三方框架
import SnapKit
import UIKit

//MARK:声明协议
protocol BPBeforeLoginViewDeletage: NSObjectProtocol {
    
    //声明方法
    //1.登录协议的方法
    func login()
    //2,注册的方法
    func register()
}

class BPBeforeLoginView: UIView {
    
    //MARK:声明代理属性
    var deletage: BPBeforeLoginViewDeletage?
    
    //MARK:在frame方法中设置视图的frame override表示重载
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        startAnimation()
        
    }
    //表示如果用手写代码这只frmae就不能再用xib或者storyboard设置
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:设置对外提供设置界面的方法
    func setBaseInfo(tip: String, imgName: String,cycleicon: UIImage) {
        bigHouse.image = UIImage(named: imgName)
        tipLabel.text = tip
        cycleIcon.image = cycleicon
    }
    
    //MARK:更新UI
    /*
     //VFL
     /**
     *  获取一个约束对象
     *
     *  @param item         需要设置约束的视图
     *  @param attribute    需要约束的属性
     *  @param relatedBy    约束优先级 一般都是 精准约束 .Equal
     *  @param toItem       约束参考的对象
     *  @param attribute    参考的属性
     *  @param multiplier   乘积系数 一般使用 1  不能够使用 0
     *  @param constant     约束值
     *
     *  @return 约束对象
     */
     //将约束对象添加到父控件上
     //手写代码添加约束 默认frame布局还是开启 就应该关闭frame布局
     // cycleIcon.translatesAutoresizingMaskIntoConstraints = false
     */
    private func  setUpUI() {
        //1.添加子控件
        addSubview(cycleIcon)
        addSubview(shadowView)
        addSubview(bigHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //2.利用第三方的库来设置UI控件的约束
        //2.1设置圈圈的约束
        cycleIcon.snp_makeConstraints { (make) in
            //make 就表示这个图标取消了frame布局的方法
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        shadowView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        bigHouse.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
            //make.center.equalTo(cycleIcon.center)
        }
        tipLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(50)
            //为了能够自动换行,约束宽度
            //make.width.equalTo(230)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX).offset(-70)
            make.centerY.equalTo(self.snp_centerY).offset(100)
            make.width.equalTo(110)
            make.height.equalTo(35)
        }
        registerBtn.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX).offset(70)
            make.centerY.equalTo(self.snp_centerY).offset(100)
            make.width.equalTo(110)
            make.height.equalTo(35)
        }
    }


    //MARk:懒加载子视图
    //圈圈  大小和图片一致没有坐标
   private lazy var cycleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
   private lazy var bigHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文字
   private lazy var tipLabel: UILabel = {
        let tip = UILabel()
        tip.textColor = UIColor.grayColor()
        tip.text = "关注一些人,回到这里看看有什么惊喜"
        //设置最大显示的宽度
        tip.preferredMaxLayoutWidth = 260
        tip.numberOfLines = 0
        tip.textAlignment = .Center
        tip.font = UIFont.systemFontOfSize(14)
        //自适应大小
        tip.sizeToFit()
        return tip
        
    }()
    //覆盖到圈圈上面的阴影视图
    private lazy var shadowView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //登录 注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //MARk:拉伸图片
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        
        //let newImg = image.stretchableImageWithLeftCapWidth(NSInteger(image.size.width*0.2), topCapHeight: NSInteger(image.size.height*0.2))
        let newImg = image.resizableImageWithCapInsets(UIEdgeInsetsMake(3, 2,3, 2), resizingMode: UIImageResizingMode.Stretch)
        //设置背景图片
        btn.setBackgroundImage(newImg, forState: .Normal)
        //添加方法
        btn.addTarget(self, action: "loginDidClick", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    
    private lazy var registerBtn: UIButton = {
        let btn1 = UIButton()
        btn1.setTitle("注册", forState: .Normal)
        btn1.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn1.titleLabel?.font = UIFont.systemFontOfSize(14)
        //MARk:拉伸图片
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        
        let newImg = image.resizableImageWithCapInsets(UIEdgeInsetsMake(3, 2,3, 2), resizingMode: UIImageResizingMode.Stretch)
        //设置背景图片
        btn1.setBackgroundImage(newImg, forState: .Normal)
        //添加方法
        btn1.addTarget(self, action:"registerDidClick", forControlEvents: .TouchUpInside)
        return btn1
    }()
    
    //MARk:利用基本动画实现旋转
    func startAnimation() {
        
        //基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        anim.toValue = 2 * M_PI
        anim.removedOnCompletion = false
        
        cycleIcon.layer.addAnimation(anim, forKey: nil)
    }
    
    //MARK:实现按钮的点击事件
   @objc private func loginDidClick() {
    
    //调用代理
    self.deletage?.login()
    }
    
   @objc private func registerDidClick() {
    //调用代理
    self.deletage?.register()
    
    }


}
